<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;
use Validator;

use Ptime3\Functions\Queue;

class TeamInformController extends Controller
{
    public function store(Request $request, $teamId)
    {
        $input = $request->all();

        //参数检验
        $validator = Validator::make($input,
            [
                'title'     => 'required|string|min:1',
                'sender_id' => 'required|integer|min:1',
                'is_urgent' => 'boolean',
                'users'     => 'required|array',
            ]
        );
        if($validator->fails())
            return $this::jsonResponse(true,$validator->messages(),"paramError");

        //创建通知
        $senderId = $input["sender_id"];
        $title    = $input["title"];
        $isUrgent = !empty($input["is_urgent"])?true:false;
        $users    = [];
        foreach ($input["users"] as $key => $value) {
            $users[] = ["user_id"=>$value["user_id"]];
        }

        $informData = [
            "title"     => $title,
            "app_id"    => env("TEAM_APP_ID"),
            "object"    => "team",
            "object_id" => $teamId,
            "sender_id" => $senderId,
            "users"     => $users,
            "is_urgent" => $isUrgent,
            "is_reply_inform" => true,
            "reply_opts"=> isset($input["reply_opts"])?$input["reply_opts"]:[
                ["content"=>"同意","is_expected"=>true],
                ["content"=>"拒绝"]
            ]
        ];

        Queue::put("restapi",["post","inform","inform/send",$informData]);

        return $this::jsonResponse(false,true);
    }

}